﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class Receipt_Cash : RibbonForm
    {
        public Receipt_Cash()
        {
            InitializeComponent();
        }

        public const int ObjectType = 13;
        public string ActiveUserID { get; set; }
        public string ConnectionString { get; set; }

        void PostDocument(string _DocNumber)
        {
            CashReceiptManager oManager = new CashReceiptManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.PostDocument(_DocNumber);

            oManager.Close();

            txtStatus.Text = "Posted";
        }

        void CancelDocument(string _DocNumber)
        {
            CashReceiptManager oManager = new CashReceiptManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CancelDocument(_DocNumber);

            oManager.Close();
            txtStatus.Text = "Canceled";
        }

        void CloseDocument(string _DocNumber)
        {
            CashReceiptManager oManager = new CashReceiptManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CloseDocument(_DocNumber);

            oManager.Close();

            txtStatus.Text = "Closed";
        }

        void InitializeSeries()
        {
            string ObjectType = "16";
            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();


            if (oSeries.IsAutoNumber(ObjectType) == true)
            {
                txtDocNum.Properties.ReadOnly = true;
                txtDocNum.Text = "##########";

            }
            oSeries.Close();
        }

        string GetNewSeries()
        {
            string _SERIES = "";
            string _OBJECT_TYPE = "16";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_OBJECT_TYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_OBJECT_TYPE);
            }
            else
            {
                _SERIES = txtDocNum.Text;
            }
            oSeries.Close();

            return _SERIES;
        }

        void DisableControls()
        {
            btnAdd.Enabled = false;
            radioSelection.Enabled = false;
            txtReference.Enabled = false;
            txtDocNum.Enabled = false;
            
            txtRemarks.Enabled = false;
            txtStatus.Enabled = false;
            dtDateCreated.Enabled = false;
            txtCreatedBy.Enabled = false;
            dtDateModified.Enabled = false;
            txtModifiedBy.Enabled = false;
            cboDocType.Enabled = false;
            txtReceiptNumber.Enabled = false;
            dtPostingDate.Enabled = false;
            txtTINNumber.Enabled = false;
            txtCustCode.Enabled = false;
            txtCustName.Enabled = false;
            txtAddress.Enabled = false;
            txtCashAmount.Enabled = false;
           

        }

        void EnableControls()
        {
            btnAdd.Enabled = true;
            txtDocNum.Enabled = true;
            radioSelection.Enabled = true;
            txtReference.Enabled = true;
            txtRemarks.Enabled = true;
            txtStatus.Enabled = true;
            dtDateCreated.Enabled = true;
            txtCreatedBy.Enabled = true;
            dtDateModified.Enabled = true;
            txtModifiedBy.Enabled = true;
            cboDocType.Enabled = true;
            txtReceiptNumber.Enabled = true;
            dtPostingDate.Enabled = true;
            txtTINNumber.Enabled = true;
            txtCustCode.Enabled = true;
            txtCustName.Enabled = true;
            txtAddress.Enabled = true;
            txtCashAmount.Enabled = true;
           
        }

        public void ShowSearch()
        {
            FND_CASHRECEIPT oForm = new FND_CASHRECEIPT();
            oForm.DocumentType = cboDocType.Text;
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            CashReceiptManager oManager = new CashReceiptManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dt= oManager.GetCashEntry(oForm.SelectedDocumentNumber);

            oManager.Close();

            DataRow oRow = dt.Rows[0];

            txtDocNum.Text = oRow["DocNum"].ToString();
            txtReference.Text = oRow["Reference"].ToString();
            txtRemarks.Text = oRow["Remarks"].ToString();
            cboDocType.Text = oRow["DocType"].ToString();
            txtReceiptNumber.Text = oRow["ReceiptNumber"].ToString();
            dtPostingDate.EditValue = (DateTime)oRow["PostingDate"];
            txtTINNumber.Text = oRow["TINNumber"].ToString();
            
            radioSelection.SelectedIndex = Convert.ToInt16(Convert.ToBoolean(oRow["IsAccount"]));
            txtCustCode.Text = oRow["PayeeCode"].ToString();
            txtCustName.Text = oRow["PayeeName"].ToString();
            txtAddress.Text = oRow["AddressOrRemarks"].ToString();
            txtCashAmount.Text = oRow["CashAmount"].ToString();

            txtStatus.Text = oRow["DocStatus"].ToString();
            txtCreatedBy.Text = oRow["CreatedBy"].ToString();
            dtDateCreated.EditValue = (DateTime)oRow["DateCreated"];
            txtModifiedBy.Text = oRow["ModifiedBy"].ToString();
            dtDateModified.EditValue = (DateTime)oRow["DateModified"];

            btnAdd.Text = "Update";
        }

        private void Receipt_Cash_Load(object sender, EventArgs e)
        {
            cboDocType.SelectedIndex = 0;

            InitializeSeries();
            
            txtCreatedBy.Text = this.ActiveUserID;
            txtModifiedBy.Text = this.ActiveUserID;

            dtDateCreated.EditValue = System.DateTime.Now;
            dtDateModified.EditValue = System.DateTime.Now;
            dtPostingDate.EditValue = System.DateTime.Now;

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                CashReceiptManager oManager = new CashReceiptManager();
                CashReceiptUnit oUnit = new CashReceiptUnit();

                txtDocNum.Text = GetNewSeries();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.Reference = txtReference.Text;
                oUnit.Remarks = txtRemarks.Text;
                oUnit.DocumentType = cboDocType.Text;
                oUnit.ReceiptNumber = txtReceiptNumber.Text;

                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.TINNumber = txtTINNumber.Text;

                if (radioSelection.SelectedIndex == 0)
                {
                    oUnit.IsAccount = false;
                }
                else
                {
                    oUnit.IsAccount = true;
                }

                oUnit.CardCode = txtCustCode.Text;
                oUnit.CardName = txtCustName.Text;
                oUnit.Address = txtAddress.Text;
                oUnit.CashAmount = Convert.ToDouble(txtCashAmount.Text);

                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.AddNewCashEntry(oUnit);

                oManager.Close();

                btnAdd.Text = "Update";


            }
            else
            {
                CashReceiptManager oManager = new CashReceiptManager();
                CashReceiptUnit oUnit = new CashReceiptUnit();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.Reference = txtReference.Text;
                oUnit.Remarks = txtRemarks.Text;
                oUnit.DocumentType = cboDocType.Text;
                oUnit.ReceiptNumber = txtReceiptNumber.Text;

                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.TINNumber = txtTINNumber.Text;

                if (radioSelection.SelectedIndex == 0)
                {
                    oUnit.IsAccount = false;
                }
                else
                {
                    oUnit.IsAccount = true;
                }

                oUnit.CardCode = txtCustCode.Text;
                oUnit.CardName = txtCustName.Text;
                oUnit.Address = txtAddress.Text;
                oUnit.CashAmount = Convert.ToDouble(txtCashAmount.Text);

                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.UpdateCashEntry(oUnit);

                oManager.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtCustCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            if (radioSelection.SelectedIndex == 0)
            {
                FND_BP _SearchBP = new FND_BP();

                AddressBookManager oBPManager = new AddressBookManager();
                TaxManager oTxManager = new TaxManager();

                try
                {


                    _SearchBP.ConnectionString = this.ConnectionString;
                    _SearchBP.BusinessPartnerType = "Customer";
                    _SearchBP.ShowDialog();

                    txtCustCode.Text = _SearchBP.CardCode;
                    txtCustName.Text = _SearchBP.CardName;
                    txtTINNumber.Text = _SearchBP.TIN;
                    txtAddress.Text = _SearchBP.Address;
                    txtAddress.Enabled = false;
                }
                catch
                {
                }
            }
            else
            {
                FND_GLACCOUNT oForm = new FND_GLACCOUNT();

                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();

                txtTINNumber.Text = "N/A";
                txtAddress.Enabled = true;
                txtCustCode.Text = oForm.SelectedGLAcctCode;
                txtCustName.Text = oForm.SelectedGLAcctName;
                
            }
        }

        private void txtCheckNo_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtCashAmount_EditValueChanged(object sender, EventArgs e)
        {

        }

        bool IsNumber(string sValue)
        {
            try
            {
                double iValue = Convert.ToDouble(sValue);
                return true;
            }
            catch
            {
                return false;
            }
        }

        
        private void txtCheckAmount_EditValueChanged(object sender, EventArgs e)
        {
           
        }

        private void txtTransferAmount_EditValueChanged(object sender, EventArgs e)
        {
           
        }

        private void txtStatus_EditValueChanged(object sender, EventArgs e)
        {
            if (txtStatus.Text == "Posted")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
            }

            if (txtStatus.Text == "Draft")
            {
                EnableControls();
                txtStatus.Properties.Buttons[0].Visible = true;
                txtStatus.Properties.Buttons[1].Visible = true;
            }

            if (txtStatus.Text == "Canceled")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
            }

            if (txtStatus.Text == "Closed")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
            }
        }

       
        private void radioSelection_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioSelection.SelectedIndex == 0)
            {
                lblSelection.Text = "Customer";
                lblAddress.Text = "Address:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";
                

            }
            else
            {
                lblSelection.Text = "Account";
                lblAddress.Text = "Remarks:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";
            }

        }

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            switch (e.Button.Index)
            {
                case 0:
                    // Post

                    PostDocument(txtDocNum.Text);

                    break;
                case 1:
                    // Cancel:
                    if (txtStatus.Text == "Draft")
                    {
                        CancelDocument(txtDocNum.Text);
                    }
                    else if (txtStatus.Text == "Posted")
                    {
                        CloseDocument(txtDocNum.Text);
                    }
                    break;
            }
        }
    }
}
